#!/bin/sh

. scripts/functions

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=susan-fd
JobName=backup-susan-fd

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

# start the test
start_test

# ${tmp}/bconcmds lists the bconsole commands executed by "run_susan"
cat <<END_OF_DATA >${tmp}/bconcmds
messages
@$out ${tmp}/log-config.out
show client=${Client}
@$out ${tmp}/log1.out w
status all
label storage=File1 pool=Default volume=TestVolume001
run job=$JobName fileset=compression-fileset storage=File1 yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/susan-restores select storage=File1
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

# Start the susan daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_susan

# verify that all are terminated
check_for_zombie_jobs storage=File1 client=${Client}

# stop susan
stop_susan

# check log files for common error messages
check_log ${tmp}/log-config.out
if ! grep -qi "TlsRequire = yes" ${tmp}/log-config.out; then
   set_error "client is not configured to use TLS."
fi

check_log ${tmp}/log1.out
check_log ${tmp}/log2.out

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
check_restore_diff ${BackupDirectory}

# do some manual testing
if [ ! -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi

# end tests and check for error codes
end_test
